﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;

namespace DL
{
    class BezierCurve
    {
        FloatPoint[] points;

        public BezierCurve(params Point[] pts)
        {
            points = new FloatPoint[pts.Length];
            for (int i = 0; i < pts.Length; i++)
                points[i] = new FloatPoint((float)pts[i].X, (float)pts[i].Y);
        }

        public FloatPoint findPoint(FloatPoint[] pts, float t)
        {
            if (pts.Length == 2)
            {
                return new FloatPoint((pts[0].x + t * (pts[1].x - pts[0].x)), (pts[0].y + t * (pts[1].y - pts[0].y)));
            }

            FloatPoint[] temp = new FloatPoint[pts.Length - 1];

            for (int i = 0; i < pts.Length - 1; i++)
            {
                temp[i] = new FloatPoint((pts[i].x + t * (pts[i + 1].x - pts[i].x)), (pts[i].y + t * (pts[i + 1].y - pts[i].y)));
            }

            return findPoint(temp, t);
        }

        public ArrayList BezierPoints()
        {
            ArrayList pts = new ArrayList();

            for (float i = 0; i < 20; i++)
                pts.Add(findPoint(points, (i / 20)));
            pts.Add(points[points.Length - 1]);

            return pts;
        }

        public void Draw(Pen pen, Graphics g)
        {
            ArrayList al = BezierPoints();
            Point[] pts = new Point[al.Count];

            int i = 0;
            foreach (FloatPoint point in al)
            {
                pts[i].X = (int)point.x;
                pts[i].Y = (int)point.y;
                i++;
            }

            g.DrawLines(pen, pts);
        }
    }
}
